package org.jeecg.modules.renlmd.service.impl;

import org.jeecg.modules.renlmd.entity.HqStRenlmd;
import org.jeecg.modules.renlmd.entity.HqStRenlmdMx;
import org.jeecg.modules.renlmd.mapper.HqStRenlmdMxMapper;
import org.jeecg.modules.renlmd.mapper.HqStRenlmdMapper;
import org.jeecg.modules.renlmd.service.IHqStRenlmdService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 后勤食堂人流密度
 * @Author: jeecg-boot
 * @Date:   2020-12-04
 * @Version: V1.0
 */
@Service
public class HqStRenlmdServiceImpl extends ServiceImpl<HqStRenlmdMapper, HqStRenlmd> implements IHqStRenlmdService {

	@Autowired
	private HqStRenlmdMapper hqStRenlmdMapper;
	@Autowired
	private HqStRenlmdMxMapper hqStRenlmdMxMapper;
	
	@Override
	@Transactional
	public void saveMain(HqStRenlmd hqStRenlmd, List<HqStRenlmdMx> hqStRenlmdMxList) {
		hqStRenlmdMapper.insert(hqStRenlmd);
		if(hqStRenlmdMxList!=null && hqStRenlmdMxList.size()>0) {
			for(HqStRenlmdMx entity:hqStRenlmdMxList) {
				//外键设置
				entity.setZhubId(hqStRenlmd.getId());
				hqStRenlmdMxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqStRenlmd hqStRenlmd,List<HqStRenlmdMx> hqStRenlmdMxList) {
		hqStRenlmdMapper.updateById(hqStRenlmd);
		
		//1.先删除子表数据
		hqStRenlmdMxMapper.deleteByMainId(hqStRenlmd.getId());
		
		//2.子表数据重新插入
		if(hqStRenlmdMxList!=null && hqStRenlmdMxList.size()>0) {
			for(HqStRenlmdMx entity:hqStRenlmdMxList) {
				//外键设置
				entity.setZhubId(hqStRenlmd.getId());
				hqStRenlmdMxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqStRenlmdMxMapper.deleteByMainId(id);
		hqStRenlmdMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqStRenlmdMxMapper.deleteByMainId(id.toString());
			hqStRenlmdMapper.deleteById(id);
		}
	}
	
}
